<?php
namespace app\index\controller;
use think\Controller;

class Auth2 extends Controller
{
    public function getuser()
    {
        $code = $_REQUEST['code'];
        $appid = config('wechat')['appid'];
        $secret = config('wechat')['secret'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $user = json_decode($this->httpGet($url));
        $openid = $user->openid;
        session('openid22', $openid);
        $url2 =  "http://".$_SERVER['HTTP_HOST'];
        $aid = session('schcodeid');
        echo "<script> location.href=\"$url2/index/Index/index?schcodeid=$aid\"; </script>";
//        $this->redirect('index/Index/index',['schcodeid'=>session('schcodeid')]);
    }
    public function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}
